package com.mocoop.ui;

import java.util.ArrayList;
import java.util.List;

import com.mocoop.activity.MocoopActivity;
import com.mocoop.entity.PageInfoEntity;

import android.view.ViewGroup;

public class MocoopLayoutAdepter {
	private List<MocoopFragment> historyList = new ArrayList<MocoopFragment>();
	private ViewGroup mainView;

	public List<MocoopFragment> getHistoryList() {
		return historyList;
	}

	public MocoopLayoutAdepter(ViewGroup mainView) {
		super();
		this.mainView = mainView;
	}

	public ViewGroup getMainView() {
		return mainView;
	}

	public void addView(MocoopFragment MocoopFragment) {
		historyList.add(MocoopFragment);
		
		if (historyList.size() > 2) {
//			mainView.removeView(historyList.get(historyList.size() - 3));
		}
	}

	public MocoopFragment goPreviousView() {
		if (historyList.size() < 2) {
			return null;
		}
		MocoopFragment destoryMocoopFragment = historyList.get(historyList.size() - 1);
		historyList.remove(destoryMocoopFragment);
//		destoryMocoopFragment.onDestroy(true);

		if (historyList.size() > 1) {
			MocoopFragment previousMocoopFragment = historyList.get(historyList.size() - 2);
//			mainView.addView(previousMocoopFragment);
		}

		MocoopFragment showMocoopFragment = historyList.get(historyList.size() - 1);
//		mainView.bringChildToFront(showMocoopFragment);
		return showMocoopFragment;

	}
	
	
	public MocoopFragment backToSpecifyPage(String targetURL) {
		for (int index = historyList.size() - 2; index < historyList.size(); index--) {
			MocoopFragment MocoopFragment = historyList.get(index);
			if (MocoopFragment.getPageInfoEntity().getUrl().equals(targetURL)) {
//				if(MocoopFragment.getParent()==null){
//					mainView.addView(MocoopFragment);
//				}
				break;
			}else{
				historyList.remove(MocoopFragment);
//				MocoopFragment.onDestroy(false);
			}
		}
			
		return goPreviousView();
	}

	
}
